<?php
namespace app\api\controller\signreport;

use app\api\controller\Baseuser;
use app\common\model\signreport\Sign as SignModel;
use app\common\model\user\Setting;
use app\api\controller\mytool\Mapapi;
use think\facade\Request;

class Sign extends Baseuser
{
    // 查询签到列表
    public function getSignList()
    {
        $date =  Request::instance()->param('SignDate');
        $where = [
            'UId' => $this->user_id,
            'SignDate' => strtotime($date)
        ];
        $signList = (new SignModel())->getList($where);
        return myok($signList);
    }

    // 签到
    public function doSign()
    {
        $params =  Request::instance()->param();
        // 获取用户信息
        $params['UId'] = $this->user_id;
        if(!empty($params['lat'])&&!empty($params['lon'])){
            //距离
            $data = (new Setting())->getSetting('sign');
            if ($data) {
                $content = json_array($data['Content'], true);
                $params['Distance'] = calculateDistance($params['lat'], $params['lon'], $content['lon_lat']['lat'], $content['lon_lat']['lat']);
            }
            //语义化地址
            $map = new Mapapi;
            $data = $map->self_get_opi($params['lat'],$params['lon']);
            if($data['code']){
                $params['Address'] = $data['msg']['address'];
            }
            $params['Lat'] = $params['lat'];
            $params['Lon'] = $params['lon'];
        }

        $res = (new SignModel())->doSign($params);
        if ($res) {
            return myok('签到成功');
        } else {
            return myerr($res);
        }
    }
    // 验证是否能签到
    public function isCheckSign()
    {
        $musttest = false;//是否强制验证所在区域

        $params =  Request::instance()->param();
        // 获取用户信息
        $result = $this->validate($params, 'Sign');
        if(true !== $result){
            return myerr($result);
        }
        // 查询参数设置
        if($musttest){
            $data = (new Setting())->getSetting('sign');
            if ($data) {
                if(!empty($content['distance'])){
                    $content = json_array($data['Content'], true);
                    $distance = calculateDistance($params['lat'], $params['lon'], $content['lon_lat']['lat'], $content['lon_lat']['lat']);
                    if ($distance < $content['distance']) {
                        $map = new Mapapi;
                        $data = $map->self_get_opi($params['lat'],$params['lon']);
                        if(!$data['code']){return myerr($data['msg']);}
                        return myok($data['msg']['address']);
                    } else {
                        return myerr('不在打卡范围');
                    }
                }else{
                    $map = new Mapapi;
                    $data = $map->self_get_opi($params['lat'],$params['lon']);
                    if(!$data['code']){return myerr($data['msg']);}
                    return myok($data['msg']['address']);
                }
            } else {
                return myerr('请联系管理员配置地址信息');
            }
        }else{
            $map = new Mapapi;
            $data = $map->self_get_opi($params['lat'],$params['lon']);
            if(!$data['code']){return myerr($data['msg']);}
            return myok($data['msg']['address']);
        }
        
    }
}
